package com.ruoyi.system.service.impl;

import com.google.api.client.util.Lists;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.CaseHistory;
import com.ruoyi.system.domain.Course;
import com.ruoyi.system.domain.DrugUpload;
import com.ruoyi.system.mapper.DrugUploadMapper;
import com.ruoyi.system.service.ICaseHistoryService;
import com.ruoyi.system.service.ICourseService;
import com.ruoyi.system.service.IDrugUploadService;
import io.minio.MinioClient;
import io.minio.policy.PolicyType;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 药品病历文件上传Service业务层处理
 *
 * @author wmb
 * @date 2021-03-01
 */
@Service
public class DrugUploadServiceImpl implements IDrugUploadService {
    @Autowired
    private DrugUploadMapper drugUploadMapper;

    @Autowired
    private ICaseHistoryService caseHistoryService;
    /**
     * 查询药品病历文件上传
     *
     * @param id 药品病历文件上传ID
     * @return 药品病历文件上传
     */
    @Override
    public DrugUpload selectDrugUploadById(Long id) {
        return drugUploadMapper.selectDrugUploadById(id);
    }

    /**
     * 查询药品病历文件上传列表
     *
     * @param drugUpload 药品病历文件上传
     * @return 药品病历文件上传
     */
    @Override
    public List<DrugUpload> selectDrugUploadList(DrugUpload drugUpload) {
        return drugUploadMapper.selectDrugUploadList(drugUpload);
    }

    /**
     * 新增药品病历文件上传
     *
     * @param drugUpload 药品病历文件上传
     * @return 结果
     */
    @Override
    public int insertDrugUpload(DrugUpload drugUpload) {
        drugUpload.setCreateTime(DateUtils.getNowDate());
        return drugUploadMapper.insertDrugUpload(drugUpload);
    }

    /**
     * 修改药品病历文件上传
     *
     * @param drugUpload 药品病历文件上传
     * @return 结果
     */
    @Override
    public int updateDrugUpload(DrugUpload drugUpload) {
        return drugUploadMapper.updateDrugUpload(drugUpload);
    }

    /**
     * 批量删除药品病历文件上传
     *
     * @param ids 需要删除的药品病历文件上传ID
     * @return 结果
     */
    @Override
    public int deleteDrugUploadByIds(Long[] ids) {
        return drugUploadMapper.deleteDrugUploadByIds(ids);
    }

    /**
     * 删除药品病历文件上传信息
     *
     * @param id 药品病历文件上传ID
     * @return 结果
     */
    @Override
    public int deleteDrugUploadById(Long id) {
        return drugUploadMapper.deleteDrugUploadById(id);
    }

    @Value("${minio.endpoint}")
    private String ENDPOINT;

    @Value("${minio.bucketname}")
    private String BUCKET_NAME;

    @Value("${minio.accessKey}")
    private String ACCESS_KEY;

    @Value("${minio.secretKey}")
    private String SECRET_KEY;

    @Override
    @Async
    public void handleDrug(Long uploadId) {
        DrugUpload drugUpload = this.selectDrugUploadById(uploadId);

        try {
            MinioClient minioClient = new MinioClient(ENDPOINT, ACCESS_KEY, SECRET_KEY);
            boolean isExist = minioClient.bucketExists(BUCKET_NAME);
            if (isExist) {

            } else {
                minioClient.makeBucket(BUCKET_NAME);
                minioClient.setBucketPolicy(BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
            }
            InputStream in = minioClient.getObject(BUCKET_NAME, drugUpload.getMessage());

            Workbook wb = WorkbookFactory.create(in);
            Sheet sheet = wb.getSheetAt(0);
            int rownum = sheet.getPhysicalNumberOfRows();


            List<CaseHistory> caseHistories = Lists.newArrayList();

            Row row = sheet.getRow(0);

            for(int i = 1; i< rownum;i++){

                CaseHistory caseHistory = new CaseHistory();
                row = sheet.getRow(i);

                //药名
                String drugName = String.valueOf(row.getCell(2));
                caseHistory.setDrugName(drugName);


                String physical = String.valueOf(row.getCell(7));
                caseHistory.setPhysical(physical);

                String inDiagnosis =  String.valueOf(row.getCell(8));
                caseHistory.setInDiagnosis(inDiagnosis);
                String outDiagnosis =  String.valueOf(row.getCell(9));
                caseHistory.setOutDiagnosis(outDiagnosis);
                String mainComplaint =  String.valueOf(row.getCell(10));
                caseHistory.setMainComplaint(mainComplaint);
                String treatmentAfter =  String.valueOf(row.getCell(11));
                caseHistory.setTreatmentAfter(treatmentAfter);
                String pathologicalResult =  String.valueOf(row.getCell(12));
                caseHistory.setPathologicalResult(pathologicalResult);
                String leaveHospitalSituation =  String.valueOf(row.getCell(13));
                caseHistory.setLeaveHospitalSituation(leaveHospitalSituation);
                String leaveHospitalOrder =  String.valueOf(row.getCell(14));
                caseHistory.setLeaveHospitalOrder(leaveHospitalOrder);
                String deathDiagnosis =  String.valueOf(row.getCell(15));
                caseHistory.setDeathDiagnosis(deathDiagnosis);
                String deathCause =  String.valueOf(row.getCell(16));
                caseHistory.setDeathCause(deathCause);
                String auxiliaryLaboratoryResult =  String.valueOf(row.getCell(17));
                caseHistory.setAuxiliaryLaboratoryResult(auxiliaryLaboratoryResult);
                String auxiliaryImagingResult =  String.valueOf(row.getCell(18));
                caseHistory.setAuxiliaryImagingResult(auxiliaryImagingResult);
                String presentIllness =  String.valueOf(row.getCell(19));
                caseHistory.setPresentIllness(presentIllness);
                String pastHistory =  String.valueOf(row.getCell(20));
                caseHistory.setPastHistory(pastHistory);
                String personalHistory =  String.valueOf(row.getCell(21));
                caseHistory.setPersonalHistory(personalHistory);
                String marriageChildbirth =  String.valueOf(row.getCell(22));
                caseHistory.setMarriageChildbirth(marriageChildbirth);
                String menstrualHistory =  String.valueOf(row.getCell(23));
                caseHistory.setMenstrualHistory(menstrualHistory);
                String familyHistory =  String.valueOf(row.getCell(24));
                caseHistory.setFamilyHistory(familyHistory);
                String physicalExamination =  String.valueOf(row.getCell(25));
                caseHistory.setPhysicalExamination(physicalExamination);
                String auxiliaryExamination =  String.valueOf(row.getCell(26));
                caseHistory.setAuxiliaryExamination(auxiliaryExamination);
                String initialDiagnosis =  String.valueOf(row.getCell(27));
                caseHistory.setInitialDiagnosis(initialDiagnosis);
                caseHistories.add(caseHistory);
            }
            caseHistoryService.insertBatchCase(caseHistories);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
